<template>
  <page-container title="收入打赏">
    <!-- 头部右侧筛选与操作 -->
    <template #extra>
      <el-date-picker
        v-model="dateRange"
        type="monthrange"
        range-separator="至"
        start-placeholder="开始月份"
        end-placeholder="结束月份"
        size="small"
        style="margin-right: 12px;"
      />
      <el-button type="primary" icon="Download" size="small">
        导出收入明细
      </el-button>
    </template>

    <div class="income-reward">
      <!-- 1. 收入统计卡片（复用风格） -->
      <el-row :gutter="20" class="summary-cards" style="margin-bottom: 24px;">
        <el-col :span="6" :xs="12" :sm="8">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-item">
              <div class="stat-value">{{ summary.monthlyIncome || 0 }}元</div>
              <div class="stat-label">本月收入</div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6" :xs="12" :sm="8">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-item">
              <div class="stat-value">{{ summary.totalRewardCount || 0 }}次</div>
              <div class="stat-label">总打赏次数</div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6" :xs="12" :sm="8">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-item">
              <div class="stat-value">{{ summary.monthlyRewardPeople || 0 }}人</div>
              <div class="stat-label">本月打赏人数</div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6" :xs="12" :sm="8">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-item">
              <div class="stat-value">{{ summary.topReward || 0 }}元</div>
              <div class="stat-label">单笔最高打赏</div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 2. 打赏特效预览（模拟动画，参考搜索到的打赏特效逻辑） -->
      <el-card shadow="hover" class="reward-effect-card" style="margin-bottom: 24px;">
        <template #header>
          <div class="effect-header">
            <span class="effect-title">打赏特效预览</span>
            <el-button type="primary" size="small" @click="triggerRewardEffect">
              触发打赏动画
            </el-button>
          </div>
        </template>
        <div class="effect-container">
          <!-- 特效容器 -->
          <div class="reward-animation">
            <div 
              class="reward-redpacket" 
              v-for="(item, index) in rewardAnimationList" 
              :key="index"
              :style="{
                left: `${item.left}%`,
                top: `${item.top}%`,
                animationDelay: `${item.delay}s`,
                animationDuration: `${item.duration}s`
              }"
              v-if="showRewardEffect"
            >
              <i class="el-icon-red-packet" style="font-size: 24px; color: #f56c6c;"></i>
            </div>
            <div class="reward-text" v-if="showRewardEffect">
              感谢 [游客12345] 的打赏！
            </div>
          </div>
          <!-- 预览说明 -->
          <div class="effect-desc" v-if="!showRewardEffect">
            <el-empty description="点击「触发打赏动画」查看效果" />
          </div>
        </div>
      </el-card>

      <!-- 3. 收入与打赏记录（标签页切换） -->
      <el-card shadow="hover" class="record-card">
        <el-tabs v-model="activeTab" type="card" style="margin-bottom: 16px;">
          <el-tab-pane label="收入明细" name="incomeDetail">
            <!-- 收入明细表格 -->
            <el-table
              :data="incomeDetailList"
              border
              size="small"
              :empty-text="emptyText"
              style="width: 100%;"
            >
              <el-table-column
                prop="date"
                label="日期"
                align="center"
                width="120"
              />
              <el-table-column
                prop="type"
                label="收入类型"
                align="center"
                width="140"
              >
                <template #default="scope">
                  <el-tag :type="scope.row.type === '签约分成' ? 'success' : 'info'">
                    {{ scope.row.type }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column
                prop="source"
                label="来源平台/作品"
                align="center"
              />
              <el-table-column
                prop="amount"
                label="金额（元）"
                align="center"
                width="120"
                formatter="formatAmount"
              />
              <el-table-column
                prop="status"
                label="状态"
                align="center"
                width="100"
              >
                <template #default="scope">
                  <el-tag :type="scope.row.status === '已到账' ? 'success' : 'warning'">
                    {{ scope.row.status }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                align="center"
                width="120"
              >
                <template #default="scope">
                  <el-button type="text" size="small">查看凭证</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>

          <el-tab-pane label="打赏记录" name="rewardRecord">
            <!-- 打赏记录表格 -->
            <el-table
              :data="rewardRecordList"
              border
              size="small"
              :empty-text="emptyText"
              style="width: 100%;"
            >
              <el-table-column
                prop="rewarder"
                label="打赏人"
                align="center"
                width="120"
              />
              <el-table-column
                prop="amount"
                label="打赏金额（元）"
                align="center"
                width="120"
                formatter="formatAmount"
              />
              <el-table-column
                prop="time"
                label="打赏时间"
                align="center"
                width="180"
              />
              <el-table-column
                prop="message"
                label="留言"
                align="center"
              />
              <el-table-column
                prop="work"
                label="打赏作品"
                align="center"
              />
              <el-table-column
                label="操作"
                align="center"
                width="120"
              >
                <template #default="scope">
                  <el-button type="text" size="small">回复感谢</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>

        <!-- 分页（模拟） -->
        <div class="pagination" style="margin-top: 20px; text-align: right;">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="activeTab === 'incomeDetail' ? incomeDetailList.length : rewardRecordList.length"
          />
        </div>
      </el-card>
    </div>
  </page-container>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import PageContainer from '@/components/PageContainer.vue';

// 模拟数据：收入统计
const summary = ref({
  monthlyIncome: 6850,
  totalRewardCount: 127,
  monthlyRewardPeople: 32,
  topReward: 1888
});

// 日期筛选
const dateRange = ref([new Date('2025-10-01'), new Date('2025-10-31')]);

// 标签页切换
const activeTab = ref('incomeDetail');
const currentPage = ref(1);
const pageSize = ref(10);
const emptyText = '暂无相关记录';

// 模拟数据：收入明细
const incomeDetailList = ref([
  {
    date: '2025-10-05',
    type: '签约分成',
    source: '起点中文网《XX修仙传》',
    amount: 2350,
    status: '已到账'
  },
  {
    date: '2025-10-12',
    type: '打赏收入',
    source: '全平台打赏汇总',
    amount: 890,
    status: '已到账'
  },
  {
    date: '2025-10-20',
    type: '签约分成',
    source: '晋江文学城《XX古言》',
    amount: 3210,
    status: '待到账'
  },
  {
    date: '2025-10-28',
    type: '其他收入',
    source: '平台奖励金',
    amount: 400,
    status: '已到账'
  }
]);

// 模拟数据：打赏记录
const rewardRecordList = ref([
  {
    rewarder: '游客12345',
    amount: 188,
    time: '2025-10-28 14:30:22',
    message: '作者大大加油！',
    work: '《XX修仙传》第58章'
  },
  {
    rewarder: '用户666888',
    amount: 66,
    time: '2025-10-27 09:15:47',
    message: '剧情太精彩了！',
    work: '《XX古言》第32章'
  },
  {
    rewarder: 'VIP用户9527',
    amount: 1888,
    time: '2025-10-25 20:42:11',
    message: '支持正版！',
    work: '《XX修仙传》第57章'
  },
  {
    rewarder: '游客54321',
    amount: 20,
    time: '2025-10-24 16:08:33',
    message: '催更催更！',
    work: '《XX古言》第31章'
  }
]);

// 打赏动画逻辑（模拟红包飘落效果）
const showRewardEffect = ref(false);
const rewardAnimationList = reactive([]);

// 生成随机动画参数
const generateAnimationParams = () => {
  const list = [];
  // 生成5个随机位置的红包
  for (let i = 0; i < 5; i++) {
    list.push({
      left: Math.floor(Math.random() * 80) + 10, // 10%-90% 横向位置
      top: -10, // 从顶部外开始
      delay: Math.random() * 0.8, // 0-0.8s 延迟
      duration: Math.random() * 2 + 2 // 2-4s 动画时长
    });
  }
  return list;
};

// 触发打赏动画
const triggerRewardEffect = () => {
  showRewardEffect.value = true;
  rewardAnimationList.length = 0;
  Object.assign(rewardAnimationList, generateAnimationParams());
  // 3秒后隐藏动画
  setTimeout(() => {
    showRewardEffect.value = false;
  }, 3000);
};

// 模拟分页方法
const handleSizeChange = (val) => {
  pageSize.value = val;
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
};

// 金额格式化（保留2位小数）
const formatAmount = (row, column) => {
  return row[column.prop].toFixed(2);
};
</script>

<style lang="scss" scoped>
.income-reward {
  // 复用统计卡片样式
  .summary-cards {
    .stat-card {
      .stat-item {
        text-align: center;
        padding: 20px 0;
        .stat-value {
          font-size: 24px;
          font-weight: bold;
          color: #409eff;
          margin-bottom: 8px;
        }
        .stat-label {
          font-size: 14px;
          color: #909399;
        }
      }
    }
  }

  // 打赏特效卡片
  .reward-effect-card {
    .effect-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .effect-title {
        font-size: 16px;
        font-weight: 500;
        color: #303133;
      }
    }
    .effect-container {
      height: 180px;
      position: relative;
      border: 1px dashed #e4e7ed;
      border-radius: 8px;
      margin-top: 12px;
      overflow: hidden;

      // 动画容器
      .reward-animation {
        width: 100%;
        height: 100%;
        position: relative;
        .reward-redpacket {
          position: absolute;
          opacity: 0;
          animation: redpacketFall 3s ease-in-out forwards;
        }
        .reward-text {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          font-size: 18px;
          color: #f56c6c;
          font-weight: bold;
          opacity: 0;
          animation: textFade 3s ease-in-out forwards;
        }
      }

      // 空状态
      .effect-desc {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}

// 红包飘落动画
@keyframes redpacketFall {
  0% {
    top: -10%;
    opacity: 1;
    transform: rotate(0deg);
  }
  80% {
    opacity: 1;
    transform: rotate(180deg);
  }
  100% {
    top: 110%;
    opacity: 0;
    transform: rotate(360deg);
  }
}

// 文字渐显动画
@keyframes textFade {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.8);
  }
  50% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.1);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.8);
  }
}

// 表格样式补充
.el-table {
  --el-table-header-text-color: #606266;
  --el-table-row-hover-bg-color: #f5f7fa;
}

// 分页样式
.pagination {
  .el-pagination {
    --el-pagination-button-size: 28px;
    --el-pagination-font-size: 12px;
  }
}
</style>